﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netfraction.Network.Protocol.BASE.Handler
{
    public class HandlerSTA : ProtocolMessageActionHandler
    {
        public enum StatusSeverity : int
        {
            Success = 0,
            RecoverableError = 1,
            FatalError = 2
        }

        // Errors x0 are generic, with the following xy errors being more specific, relating to x
        public enum StatusErrorCode : int
        {
            Generic = 0,
            GenericHubError = 10,
            HubFull = 11,
            HubDisabled = 12,
            GenericLoginError = 20,
            NickInvalid = 21,
            NickTaken = 22,
            InvalidPassword = 23,
            ClientIDTaken = 24,
            CommandDenied = 25,
            RegisteredUsersOnly = 26,
            InvalidPrivateID = 27,
            GenericKickBanDisconnect = 30,
            PermanentlyBanned = 31,
            TemporarilyBanned = 32,
            ProtocolError = 40,
            TransferProtocolUnsupported = 41,
            DirectConnectionFailed = 42,
            RequiredINFFieldMissingOrBad = 43,
            InvalidState = 44,
            RequiredFeatureMissing = 45,
            InvalidIP = 46,
            NoCommonHashFeatureWithHub = 47,
            C2COrFileTransferError = 50,
            FileNotAvailable = 51,
            FilePartNotAvailable = 52,
            SlotsFull = 53,
            NoCommonHashFeatureWithClient = 54
        }

        public HandlerSTA()
            : base()
        {
            mParameterCodes = new List<string>(new string[] { "FC", "TL", "TO", "PR", "FM", "FB", "I4", "I6" });

            mSupportedConnectionStates = new List<RemoteMachine.ConnectionState>();
        }

        #region ProtocolMessageActionHandler Members

        public override string ProtocolActionCode
        {
            get { return "STA"; }
        }

        public override bool HandleProtocolMessage(Netfraction.RemoteMachine.Node node, ProtocolMessage message, IEnumerable<ProtocolFeature> extenders)
        {
            // STA code description - where code = xyy, x = severity, yy = error code
            if (message.Parameters.Count >= 2)
            {
                StatusSeverity severity = (StatusSeverity) Enum.Parse(typeof(StatusSeverity), message.Parameters[0].ParameterValue.Substring(0, 1));
                StatusErrorCode errorCode = (StatusErrorCode)Enum.Parse(typeof(StatusErrorCode), message.Parameters[0].ParameterValue.Substring(1, 2));

                string description = message.Parameters[1].ParameterValue;
                // This is a hack for the REF extension in the situation where it includes no desciption. May be able/need to change ProtocolMessage parser.
                if (description.StartsWith("RF", StringComparison.InvariantCulture) && description.Length > 2)
                {
                    ClientInterface.Chat.ChatMessageReceived(node.Identity, null, null, string.Format("Referrer: {0}", description.Substring(2)), Netfraction.ClientInterface.ChatOptions.HubMessage);
                }
                else
                {
                    ClientInterface.Chat.ChatMessageReceived(node.Identity, null, null, description, Netfraction.ClientInterface.ChatOptions.HubMessage);
                }
                return true;
            }
            return false;
        }

        #endregion
    }
}
